﻿package birdry.core
{
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	
	import birdry.utils.ColorUtil;
	import birdry.utils.DisplayObjectUtil;
	
	import birdry.core.ColorPickerEvent;
	
	/**
	* ...
	* @author 
	*/
	public class ColorPicker	extends EventDispatcher		{
		
		var cpMc:MovieClip;
		var bitmapData:BitmapData;
		public var colorStr:String;
		public var colorUint:uint;
		
		public function ColorPicker(cpMc: MovieClip): void	{
			this.cpMc = cpMc;
			setLayout();
			addEvent();
		}
		
		private function setLayout(): void	{
			bitmapData = new BitmapData(cpMc.width, cpMc.height);
			bitmapData.draw(cpMc);
		}
		
		private function addEvent(): void	{
			cpMc.addEventListener(MouseEvent.MOUSE_OVER, onOver);
			cpMc.addEventListener(MouseEvent.MOUSE_OUT, onOut);
			cpMc.addEventListener(MouseEvent.CLICK, onClick);
		}
		
		private function onOver( e: MouseEvent ): void	{
			cpMc.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
			dispatchEvent(new ColorPickerEvent(ColorPickerEvent.OVER) );
		}
		
		private function onMove( e: MouseEvent ): void	{
			var rgb: uint = bitmapData.getPixel( cpMc.mouseX, cpMc.mouseY );
			dispatchEvent(new ColorPickerEvent(ColorPickerEvent.MOVE, rgb) );
		}
		
		
		private function onOut( e: MouseEvent ): void	{
			cpMc.removeEventListener(MouseEvent.MOUSE_MOVE, onMove);
			dispatchEvent(new ColorPickerEvent(ColorPickerEvent.OUT) );
		}
		
		private function onClick( e: MouseEvent ): void	{
			var rgb: uint = bitmapData.getPixel( cpMc.mouseX, cpMc.mouseY );
			colorUint = rgb;
			colorStr = "0x" + ColorUtil.toRGBString(rgb.toString( 16 ).toUpperCase());
			dispatchEvent(new ColorPickerEvent(ColorPickerEvent.CLICK) );
		}
	}	// class
	
}	// package